@extends('layouts.default')

@section('title', '班主任')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var termId = 0;
        var termTitle = "";
        
        renderSelect();

        laydate.render({
            elem: '#award-date'
            ,type: 'date'
            ,range: true
        });

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            
            $("#termSelector").val(termId);
            form.render();

            getList(1, per_page)
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            var para = {
                "page": page,
                "search": filters,
                "limit": limit,
            };

            getTeacherList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            // {field: 'term_name', title: '学年学期', align: 'left', templet: function(d) {
                            //     let termName = d.data_version_id ? d.term.title : '-';
                            //     return d.data_version_name ? d.data_version_name : termName;
                            // }},
                            {field: 'term_name', title: '学年学期', align: 'left', templet: function(d) {
                                return termTitle;
                            }},
                            {field: 'major_subject_name', title: '主学科', align: 'center', templet: function(d) {
                                return d.major_subject_array.length > 0 ? d.major_subject_array[0].selectValue : '-';
                            }},
                            {field: 'name', title: '教师姓名', align: 'center'},
                            {title: '操作', width: '20%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            // setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'add':
                    
                break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;
            
            if(obj.event === 'download') {
                downloadTeacherInfo({user_id: data.user_id, term_id: termId},function(res){
                    if(res.code == '1'){
                        exportsDel(res.data.show_name, '/download/excel/'+res.data.file_name);
                    }else{
                        layer.msg(res.msg, {icon: 5,shift : 6});
                    }
                });
            }
        });

        form.on('select(termList)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象

            termId = data.value;
            termTitle = data.elem[data.elem.selectedIndex].text;

            getList(1, per_page);
        });

        function exportsDel(showName,fileName){
            window.location.href = "{{ route('download.FileDownloadDel') }}?filename="+showName+"&file_url="+fileName;
        }

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            getList(1, per_page);
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['TEACHER_SUBJECT']}, function (res){
                    if(res.code === 1) {  
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];
                            
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }
                        
                        form.render(); 
                        
                        resolve();
                    }
                });
            });
        }
        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        for(var i in res.data){
                            if(i<=0) {
                                termId = res.data[i].id;
                                termTitle = res.data[i].title;
                            }
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                      
                        form.render('select');

                        resolve();
                    }
                });  
            });
        } 

    });
</script>
@endpush

@section('content')
<style>
    
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <form id="qryForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline"> 
                        <label class="layui-form-label">学年学期：</label>
                        <div class="layui-input-inline">
                            <select name="data_version_id" id="termSelector" lay-filter="termList" data-role="termList"></select>
                        </div>
                    </div>
                    <div class="layui-inline"> 
                        <label class="layui-form-label">主学科：</label>
                        <div class="layui-input-inline">
                            <select name="subject" lay-filter="teacher_subjectList" data-role="teacher_subjectList"></select>
                        </div>
                    </div>
                    <div class="layui-inline" >
                        <label class="layui-form-label">教师姓名：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="请选择" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" lay-submit lay-filter="queryBtn" class="layui-btn layui-btn-normal">查询</button>
                        <button type="button" lay-submit lay-filter="resetBtn" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>

<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>教师列表</h3>
            <div class="layui-btn-container list-opt-btns">
                <!-- <button btnPermission="" class="layui-btn layui-btn-normal" type="button" lay-event="add"> + 新增 </button> -->
            </div>
        </div>
    </script>

    <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
    <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>

    <script type="text/html" id="opDemo">
        <a btnPermission="" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="download">下载</a>
    </script>
</div>
  
@endsection